package com.blockmeta.bbs.businesslibrary.net.okhttp;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
 * Created by author on 2018/4/20.
 * <p>
 * 网络加载错误之后的重试
 */

public class AdapterLoadDataBusiness {
    public static void LoadErrorData(Context context, BaseAdapter adapter, RecyclerView list_recy, final ReLoadNet reLoadNet) {
        ProgressBarUtil.dismissProgress(context);
        View errorView = LayoutInflater.from(context).inflate(R.layout.empty_net, null);
        ImageView reconn = (ImageView) errorView.findViewById(R.id.click_reconn);
        reconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reLoadNet.retryLoadNet();
            }
        });
        adapter.setEmptyView(errorView);
        list_recy.setAdapter(adapter);

    }
    public static void LoadErrorData(Context context, BaseMultiItemQuickAdapter adapter, RecyclerView list_recy, final ReLoadNet reLoadNet) {
        ProgressBarUtil.dismissProgress(context);
        View errorView = LayoutInflater.from(context).inflate(R.layout.empty_net, null);
        ImageView reconn = (ImageView) errorView.findViewById(R.id.click_reconn);
        reconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reLoadNet.retryLoadNet();
            }
        });
        adapter.setEmptyView(errorView);
        list_recy.setAdapter(adapter);

    }
    public static void LoadErrorData(Context context, BaseQuickAdapter adapter, RecyclerView list_recy, final ReLoadNet reLoadNet) {
        if(context == null){
            return;
        }
        View errorView = LayoutInflater.from(context).inflate(R.layout.empty_net, null);
        ImageView reconn = (ImageView) errorView.findViewById(R.id.click_reconn);
        reconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reLoadNet.retryLoadNet();
            }
        });
        adapter.setHeaderFooterEmpty(true,true);
        adapter.setEmptyView(errorView);
    }

    public interface ReLoadNet {
        void retryLoadNet();
    }

    public static void LoadEmptyData(Context context, BaseQuickAdapter adapter) {
        if (adapter == null) return;
        View empty = LayoutInflater.from(context).inflate(R.layout.empty_data, null);
        adapter.setEmptyView(empty);
    }

    public static void addFooterView(Context context, BaseQuickAdapter adapter, int resId) {
        if (adapter == null) return;
        View footerView = LayoutInflater.from(context).inflate(resId, null);
        adapter.addFooterView(footerView);
    }

    public static void finishLoadAndRefresh(SmartRefreshLayout list_swipe) {
        if (list_swipe == null) return;
        if (list_swipe.isLoading()) list_swipe.finishLoadmore();
        if (list_swipe.isRefreshing()) list_swipe.finishRefresh();
    }

}
